package com.normalClasses.eye;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileManipulation {
  public static void copy(String file1,String file2){
    File fileOne=new File(file1);
    File fileTwo=new File(file2);
    try {
      FileReader fis=new FileReader(fileOne);
      BufferedReader bis=new BufferedReader(fis);
      FileWriter fos=new FileWriter(fileTwo);
      BufferedWriter bos = new BufferedWriter(fos);
      String line =bis.readLine();
      while(line!=null) {
        bos.write(line);
        bos.newLine();
        bos.flush();
        line=bis.readLine();
      }
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch(IOException e) {
      e.printStackTrace();
    }
    }
  
  public static String getLastLine(String file){
    String lastLine="";
    String line="";
    FileReader fis;
    try {
      fis = new FileReader(new File(file));
      BufferedReader bis= new BufferedReader(fis);
      line =bis.readLine();
      while(line!=null){
        lastLine=line;
        line=bis.readLine();
      }
    }catch(FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return lastLine;    
  }
 
public static void getNewLogPart(String file1,String file2){
    //String lastLine = getLastLine(file2);
    //System.out.println("lastLine"+lastLine);
    File fileOne=new File(file1);
    File fileTwo=new File(file2);
    //String lastLine = getLastLine(file2);
    //System.out.println("lastLine"+lastLine);
    String currentLine="";
    String lastLine="";
    try{
      lastLine = getLastLine(file2);
      //System.out.println("lastLine"+lastLine);
      boolean flag=false;
      FileReader fis=new FileReader(fileOne);
      BufferedReader bis=new BufferedReader(fis);
      FileWriter fos=new FileWriter(fileTwo);
      BufferedWriter bos = new BufferedWriter(fos);
     // lastLine = getLastLine(file2);
     //System.out.println("lastLine"+lastLine);
      currentLine=bis.readLine();
      //System.out.println("currentLine"+currentLine);
      if(currentLine.equals(lastLine)){
        
    	  flag=true;
      }
      if(lastLine.equals("")){
        copy(file1,file2);
      }
      else{
    while(flag==false&&currentLine!=null) {
        currentLine=bis.readLine();
        
        if(currentLine!=null) {
        
        if(currentLine.equals(lastLine)){
          flag=true;
        }
        
        }
        
    }
      
        while(currentLine!=null){
          currentLine=bis.readLine();
          if(currentLine==null){
            break;
          }
          bos.write(currentLine);
          bos.write("\n");
          bos.flush();
          //currentLine=bis.readLine();
        }
      }
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch(IOException e) {
      e.printStackTrace();
    }
  }
  
}
